package org.hang.jvm;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Optional;

/**
 * 说明
 *
 * @author 廖景航
 * @date created on 2021/10/5 下午4:52
 */
@Getter
@Setter
public class Cmd {

    @Parameter(names = {"?", "-help"}, description = "print help message", order = 3, help = true)
    private boolean helpFlag = false;

    @Parameter(names = {"-version"}, description = "print version and exit", order = 2)
    private boolean versionFlag = false;

    @Parameter(names = {"-cp", "-classpath"}, description = "classpath", order = 1)
    private String classpath;

    @Parameter(names = "-Xjre", description = "path to jre", order = 4)
    private String jre;

    @Parameter(description = "main class and args")
    private List<String> mainClassAndArgs;

    private boolean ok;

    public String getManClass() {
        return Optional.ofNullable(this.mainClassAndArgs).map(args -> args.get(0)).orElse(null);
    }

    public List<String> getArgs() {
        return Optional.ofNullable(this.mainClassAndArgs).map(args -> args.subList(1, args.size())).orElse(null);
    }

    static Cmd parse(String[] argv) {
        Cmd cmd = new Cmd();
        JCommander jCommander = JCommander.newBuilder().addObject(cmd).build();
        jCommander.parse(argv);
        cmd.ok = true;
        return cmd;
    }


}
